//package com.neusoft.elmboot.service.impl;
//
//import com.neusoft.elmboot.service.AiService;
//import org.springframework.stereotype.Service;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class AiServiceImpl implements AiService {
//
//	@Override
//	public String listOptionByAdj(String adj) {
//		System.out.println(adj);
//		Process proc;
//		List<String> list = new ArrayList();
//		try {
//			String pythonPath = "D:\\Program Files (x86)\\Python\\python.exe";
//			String pythonFilePath = "E:\\ElmPractice\\elmboot\\gpt_api\\test.py";
//			String[] args1 = new String[] { pythonPath, pythonFilePath, adj};
//			System.out.println("step1");
//			proc = Runtime.getRuntime().exec(args1);
//
//			System.out.println("step2");
//			/*
//			 * String数组里的那一行很重要 首先一定要设置好你所使用的python的位置，切记不要直接使用python，因为系统会默认使用自带的python，
//			 * 所以一定要设置好你所使用的python的位置，
//			 * 否则可能会出现意想不到的问题（比如说我使用的是anaconda中的python，而ubuntu系统会默认调用自带的python，
//			 * 而我自带的python中并没有numpy库，所以会造成相应的代码不会执行的问题，所以设置好python的位置是很重要的）。
//			 * 还有就是要设置好py文件的位置，使用绝对路径。在这里插入代码片 还有就是可以看出，此方法可以满足我们python代码中调用第三方库的情况，简单实用。
//			 */
//			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
//			String line = null;
//			while ((line = in.readLine()) != null) {
//				System.out.println(line);
//				list.add(line);
//			}
//			in.close();
//
//			// 捕获错误输出流
//			BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream(), "UTF-8"));
//			String errorLine = null;
//			while ((errorLine = err.readLine()) != null) {
//				System.out.println("ERROR: " + errorLine);
//			}
//			err.close();
//
//			int r=proc.waitFor();
//			System.out.println(r);
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//		System.out.println(list);
//		String res="";
//		for(String c:list) {
//			res+=c;
//		}
//		return res;
//	}
//
//	@Override
//	public List<String> getContinuousAnswer(String question) {
//		Process proc;
//		List<String> list = new ArrayList();
//		try {
//			String[] args1 = new String[] { "D:\\Program Files (x86)\\Python\\python.exe",
//					"E:\\ElmPractice\\elmboot\\gpt_api\\test.py", question};
//			proc = Runtime.getRuntime().exec(args1);
//			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
//			StringBuffer buf = new StringBuffer();
//			String line = null;
//			while ((line = in.readLine()) != null);
//        }catch(Exception e) {
//
//        }
//		return list;
//	}
//
//}

package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.service.AiService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Service
public class AiServiceImpl implements AiService {

	@Override
	public String listOptionByAdj(String adj) {
		System.out.println(adj);
		Process proc;
		List<String> list = new ArrayList<>();
		try {
			// 修改为服务器上的 Python 路径
			String pythonPath = "/usr/bin/python3.6";
			// 修改为服务器上的 test.py 文件路径
			String pythonFilePath = "/www/wwwroot/http/gpt_api/test.py";
			String[] args1 = new String[] { pythonPath, pythonFilePath, adj };
			System.out.println("step1");

			// 使用 ProcessBuilder
			ProcessBuilder pb = new ProcessBuilder(args1);
			pb.redirectErrorStream(true); // 合并标准错误流和标准输出流
			proc = pb.start();

			System.out.println("step2");
			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
				list.add(line);
			}
			in.close();

			int exitCode = proc.waitFor();
			System.out.println("Process exited with code: " + exitCode);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(list);
		return String.join("", list);
	}

	@Override
	public List<String> getContinuousAnswer(String question) {
		Process proc;
		List<String> list = new ArrayList<>();
		try {
			String pythonPath = "/usr/bin/python3.6";
			String pythonFilePath = "/www/wwwroot/http/gpt_api/test.py";
			String[] args1 = new String[] { pythonPath, pythonFilePath, question };

			// 使用 ProcessBuilder
			ProcessBuilder pb = new ProcessBuilder(args1);
			pb.redirectErrorStream(true); // 合并标准错误流和标准输出流
			proc = pb.start();

			BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
				list.add(line);
			}
			in.close();

			int exitCode = proc.waitFor();
			System.out.println("Process exited with code: " + exitCode);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		return list;
	}
}

